[EF 4 POCO] Problem with INSERT...
Posted
by Darmak
on Stack Overflow
See other posts from Stack Overflow
or by Darmak
Published on 2010-03-28T10:40:14Z
Indexed on
2010/03/28
10:43 UTC
Read the original article
Hit count: 346
Hi all, I'm so frustrated because of this problem, you have no idea...
I have 2 classes: Post and Comment. I use EF 4 POCO support, I don't have foreign key columns in my .edmx model (Comment class doesn't have PostID property, but has Post property)
class Comment {
public Post post { get; set; }
// ...
}
class Post {
public virtual ICollection<Comment> Comments { get; set; }
// ...
}
Can someone tell me why the code below doesn't work? I want to create a new comment for a post:
Comment comm = context.CreateObject<Comment>();
Post post = context.Posts.Where(p => p.Slug == "something").SingleOrDefault();
// post != null, so don't worry, be happy
// here I set all other comm properties and...
comm.Post = post;
context.AddObject("Comments", comm); // Exception here
context.SaveChanges();
The Exception is:
Cannot insert the value NULL into column 'PostID', table 'Blog.Comments'; column does not allow nulls. INSERT fails.
... this 'PostID' column is of course a foreign key to the Posts table.
Any help will be appreciated!
© Stack Overflow or respective owner